from flask import request
from config import app, redis_service
from services import user_service, sell_service, food_service, produce_service, buy_service, ship_service, \
    member_service,analysis_service

from models.create_order import CreateOrder
from models.create_producer import CreateProducer
from models.create_buy import CreateBuy
from models.create_transport import CreateTransport
from results import *


@app.before_request
def before():
    url = request.path
    if url == '/login' or url == '/register':
        pass
    else:
        token = request.headers.get('token')
        if token is None:
            return not_login_result
        username = redis_service.get(token)
        if username:
            pass
        else:
            return not_login_result


@app.post('/login')
def login():
    get_json = request.json
    return user_service.login_impl(get_json)


@app.post('/register')
def register():
    get_json = request.json
    return user_service.register_impl(get_json)


@app.post('/user_info_change')
def user_info_change():
    get_json = request.json
    return user_service.update_user_info(get_json)


@app.post('/create_food')
def create_food():
    get_json = request.json
    return food_service.create_food(get_json, request)


@app.post('/get_food')
def get_food():
    get_json = request.json
    return food_service.get_food(get_json, request)


@app.post('/query_food')
def query_food():
    get_json = request.json
    return food_service.query_food(get_json)


@app.post('/create_order')
def create_order():
    get_json = request.json
    return sell_service.create_order(get_json, request)


@app.post('/get_order')
def get_order():
    get_json = request.json
    return sell_service.get_order(get_json, request)


@app.post('/query_order')
def query_order():
    get_json = request.json
    return sell_service.query_order(get_json)


@app.post('/create_production_order')
def create_production_order():
    get_json = request.json
    return produce_service.create_production_order(get_json, request)


@app.post('/get_production_order')
def get_production_order():
    get_json = request.json
    return produce_service.get_production_order(get_json, request)


@app.post('/query_production_order')
def query_production_order():
    get_json = request.json
    return produce_service.query_order(get_json)


@app.post('/create_buy_order')
def create_buy_order():
    get_json = request.json
    return buy_service.create_buy_order(get_json, request)


@app.post('/get_buy_order')
def get_buy_order():
    get_json = request.json
    return buy_service.get_buy_order(get_json, request)


@app.post('/query_buy_order')
def query_buy_order():
    get_json = request.json
    return buy_service.query_order(get_json)


@app.post('/create_ship_order')
def create_ship_order():
    get_json = request.json
    return ship_service.create_transport_order(get_json, request)


@app.post('/get_ship_order')
def get_ship_order():
    get_json = request.json
    return ship_service.get_transport_order(get_json, request)


@app.post('/query_transport_order')
def query_transport_order():
    get_json = request.json
    return ship_service.query_order(get_json)


@app.post('/query_member')
def query_member():
    get_json = request.json
    return member_service.query_member(get_json)


@app.get("/order_line_chart")
def order_line_chart():
    return analysis_service.line_order_service()


@app.get("/food_column_chart")
def food_column_chart():
    return analysis_service.column_service()


@app.get("/food_pie_chart")
def food_pie_chart():
    return analysis_service.food_pie_service()


@app.get("/order_sum")
def order_sum():
    order = CreateOrder.query.count()
    producer = CreateProducer.query.count()
    buy = CreateBuy.query.count()
    transport = CreateTransport.query.count()
    return gen_result(200, {
        'order': order,
        'produce':producer,
        'buy':buy,
        'transport':transport
    })

if __name__ == '__main__':
    app.run(debug=True)
